from pprint import pprint

import requests
from lxml import etree


# 获取网页
def get_page():
    url = f'https://www.douban.com/group/explore?tdsourcetag=s_pctim_aiomsg&qq-pf-to=pcqq.group'
    headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
    }
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        # response.content返回的是字节流
        return response.content.decode('utf-8')
    return None


# 使用xpath解析网页
def parse_page(html):
    # 把html文本对象转换成etree节点对象
    etree_html = etree.HTML(html)
    # print(etree_html)
    # print(type(etree_html))

    # // 表示匹配子孙们，* 表示任意类型节点，//* 子孙们任意类型节点
    # results = etree_html.xpath('//img')
    # pprint(results)
    # print(len(results))

    # 所有a标签
    # results = etree_html.xpath('//a/text()')
    # pprint(results)
    # print(len(results))

    # results = etree_html.xpath('//div/h3/a/text()')
    # pprint(results)
    # print(len(results))

    # class
    # results = etree_html.xpath('//div[@class="likes"]/text()')
    # pprint(results[::2])
    # print(len(results))

    # 元素属性
    # results = etree_html.xpath('//div[@class="pic"]/div[@class="pic-wrap"]/img/@src')
    # pprint(results)
    # print(len(results))

    # and or
    # results = etree_html.xpath('//span[@class="pubtime" and contains(text(),"昨天")]/text()')

    # 获取昨天16到18的数据
    # results = etree_html.xpath('//span[@class="pubtime" and starts-with(text(),"昨天16")]/../../h3/a/text()')

    # 3到6条数据
    # results = etree_html.xpath('//div[@class="channel-item"][position()>2 and position() < 6]/div/h3/a/text()')

    # results = etree_html.xpath('')
    # following::* 当前节点之后的所有节点
    # following-sibling::* :当前节点之后同级节点，不包括自身
    results = etree_html.xpath('//div[@class="channel-item"][2]/following-sibling::*')

    # out of range，先取大的记录，再在记录里面去取条目
    channel_item = etree_html.xpath('//div[@class="channel-item"]')
    print(len(channel_item))



    pprint(results)
    print(len(results))


def main():
    html = get_page()
    # print(html)
    parse_page(html)


if __name__ == '__main__':
    main()
